
//
//  HTComposeToolBar.swift
//  weibo
//
//  Created by JonsonLiu on 16/4/26.
//  Copyright © 2016年 liujonson. All rights reserved.
//

import UIKit

enum HTComposeToolBarType : Int{

    case picture = 0, Mention , Trend , Emoticon ,Add
}

@available(iOS 9.0, *)
class HTComposeToolBar: UIStackView {

    
    var buttonClickClosed : ((type: HTComposeToolBarType) ->())?
    
    var isSystemKeyBoard : Bool = true {
    
        didSet{
        
            let  button = self.viewWithTag((HTComposeToolBarType.Emoticon).rawValue) as? UIButton
            
            var name = "compose_keyboardbutton_background"
            
            if isSystemKeyBoard{
            
                name = "compose_emoticonbutton_background"
            }
            
            button?.setImage(UIImage(named: "\(name)_highlighted"), forState: .Highlighted)
            button?.setImage(UIImage(named: name), forState: .Normal)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
        distribution = .FillEqually
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        addChildButton("compose_toolbar_picture", type: .picture)
        addChildButton("compose_mentionbutton_background", type: .Mention)
        addChildButton("compose_trendbutton_background",type : .Trend)
        addChildButton("compose_emoticonbutton_background",type : .Emoticon)
        addChildButton("compose_add_background",type : .Add)
        
    }
    
    
    
    private func addChildButton(imageNamed : String , type : HTComposeToolBarType){
        
        let  button = UIButton()
        
        button.addTarget(self, action: "childButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.tag = type.rawValue
        
        button.setImage(UIImage(named: imageNamed), forState: .Normal)
        
        button.setImage(UIImage(named: "\(imageNamed)_highlighted"), forState: .Highlighted)
        
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: .Normal)
        
        button.setBackgroundImage(UIImage(named:"compose_toolbar_background"), forState: .Highlighted)
        
        addArrangedSubview(button)
        
    }
    
    
    @objc private func childButtonClick(button : UIButton){
    
//        buttonClickClosed?(type:HTComposeToolBarType(rawValue: button.tag)!)
        buttonClickClosed?(type : HTComposeToolBarType(rawValue: button.tag)!)
    }
}
